#pragma once
#include <glm/glm.hpp>
#include "../opengl/opengl.hpp"

struct AxisHelper {
    static constexpr auto vs_src =
        "#version 450 core\n"
        "layout(location=0)in vec3 pos;layout(location=0)out vec4 color;"
        "struct World{vec3 cameraPos;mat4 matVP;};layout(binding=0,std140)uniform WorldBlock{World world;};"
        "void main(){gl_Position=world.matVP*vec4(pos,1.0);const vec3 colors[]={vec3(1,0,0),vec3(0,1,0),vec3(0,0,1)};"
        "color=vec4(colors[gl_VertexID>>1],1.0);}";
    static constexpr auto fs_src =
        "#version 450 core\n"
        "layout(location=0)in vec4 color;layout(location=0)out vec4 fragColor;void main(){fragColor=color;}";

    struct {
        glm::vec3 x1{0.0}, x2{0.0}; // Red
        glm::vec3 y1{0.0}, y2{0.0}; // Green
        glm::vec3 z1{0.0}, z2{0.0}; // Blue
    } data;
    opengl::buffer vertices;
    opengl::vertex_array vao;
    opengl::program program;

    AxisHelper(float size = 1.0)
        : vertices(sizeof(data)), vao({opengl::vertex_attribute(0, opengl::type_f32, 3, 0)}),
          program({opengl::shader(opengl::shader::vertex_shader, vs_src),
                   opengl::shader(opengl::shader::fragment_shader, fs_src)}) {
        data.x1.x = -size;
        data.x2.x = size;
        data.y1.y = -size;
        data.y2.y = size;
        data.z1.z = -size;
        data.z2.z = size;
        vertices.set_data(&data, sizeof(data));
    }
    void render() const {
        // opengl::api::glDisable(0x0B71);
        vao.bind();
        program.use();
        vao.set_vertex_buffer(vertices, sizeof(glm::vec3));
        opengl::api::glDrawArrays(opengl::mode_lines, 0, 6);
    }
};
